package com.weishenai.customerassistant.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.io.File;
import java.util.concurrent.ThreadPoolExecutor;

/**
 * @Auther: weishenai
 * @Date: 2021/11/28/11:30
 * @Description: 解决跨域 静态资源映射
 */
@Slf4j
@Configuration
public class CorsConfig implements WebMvcConfigurer {
    @Value("${file.iconPath}")
    private String iconSavePath;

    @Value("${file.imageSavePath}")
    private String fileSavePath;

    private CorsConfiguration buildConfig() {
        CorsConfiguration corsConfiguration = new CorsConfiguration();
        corsConfiguration.addAllowedOrigin("*");
        corsConfiguration.addAllowedHeader("*");
        corsConfiguration.addAllowedMethod("*");
        corsConfiguration.addExposedHeader("Authorization");
        return corsConfiguration;
    }

    @Bean
    public CorsFilter corsFilter() {
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", buildConfig());
        return new CorsFilter(source);
    }

    /***
     * @Description: 解决跨域问题
     * @Param: [registry]
     * @return: void
     * @Author: weishenai
     * @Date: 2021/12/15
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("GET", "POST", "DELETE", "PUT")
                .maxAge(3600);
    }

    /***
     * @Description: 静态资源映射
     * @Param: [registry]
     * @return: void
     * @Author: weishenai
     * @Date: 2021/12/15
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //排除静态文件
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("doc.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");

        if (!iconSavePath.endsWith(File.separator)) {
            iconSavePath += File.separator;
        }
        registry.addResourceHandler("/userIcon/**")
                .addResourceLocations("file:" + iconSavePath);

        //图片映射
        registry.addResourceHandler("/image/**")
                .addResourceLocations("file:" + fileSavePath);
    }


    /**
     * 线程池
     */
    @Bean("executor")
    public ThreadPoolTaskExecutor threadPoolTaskExecutor() {
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(Runtime.getRuntime().availableProcessors());
        executor.setMaxPoolSize(128);
        executor.setQueueCapacity(1024);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.DiscardPolicy());
        executor.setWaitForTasksToCompleteOnShutdown(true);
        executor.setAwaitTerminationSeconds(3);
        return executor;
    }
}
